import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.apache.commons.lang.StringUtils;

import com.google.common.base.Function;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class gaView_nowait {
	static WebDriver driver;
	static Wait<WebDriver> wait;
	public static void main(String[] args) {
		driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
		driver.get("https://gsw8.view.usg.edu");

		try {
			driver.findElement(By.name("webctid")).sendKeys("sbaev");
			driver.findElement(By.name("password")).sendKeys("123456\n");
			driver = driver.switchTo().frame("APPLICATION_FRAME");
			driver = driver.switchTo().frame("TOOLFRAME");
			driver.findElement(By.partialLinkText("Training Section_Baev")).click();
			driver = driver.switchTo().defaultContent().switchTo().frame("APPLICATION_FRAME");
			driver = driver.switchTo().frame("TOOLFRAME");
			driver = driver.switchTo().frame("RIGHTTOOLFRAME");
			
			driver.findElement(By.partialLinkText("Assignments")).click();
			WebElement aItem = driver.findElements(By.partialLinkText("Assignment")).get(0);
			String aName = aItem.getText();
			aItem.click();
			String headerLine = String.format("%-20s%-30s%-30s%-30s\n","Assignment","Username","Due date","Submission Date");
			headerLine += StringUtils.repeat("-",headerLine.length());
			System.out.println(headerLine);
			for(WebElement sItem : driver.findElements(By.partialLinkText(aName))) {
				WebElement rItem = sItem.findElement(By.xpath("../../.."));
				String fullName = rItem.findElement(By.xpath("td[3]")).getText();
				System.out.printf("%-20s%-30s%-30s%-30s\n",
					aName,
					fullName.substring(fullName.indexOf('(')+1, fullName.indexOf(')')),
					rItem.findElement(By.xpath("td[5]")).getText(),
					rItem.findElement(By.xpath("td[6]")).getText()
				);
			}		
			
					
			// Retrieve cookies
			Cookie c1 = driver.manage().getCookieNamed("uid2");
			Cookie c2 = driver.manage().getCookieNamed("BIGipServeru4_vista_webapp");
			Cookie c3 = driver.manage().getCookieNamed("JSESSIONIDVISTA");
			String myCookie = String.format("%s=%s; %s=%s; %s=%s",c1.getName(),c1.getValue(),c2.getName(),c2.getValue(), c3.getName(),c3.getValue());
			// Open separate URL connection to the server and pass the obtained cookies			
			/*
			URL url = new URL("https://gsw8.view.usg.edu/webct/urw/tp4593330069261.lc4581496208331/RelativeResourceManager/sfsid/4640624213311");
			HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
			httpConn.setAllowUserInteraction(false);
			httpConn.setInstanceFollowRedirects(true);
			httpConn.setRequestMethod("GET");
			httpConn.setConnectTimeout(10000);
			httpConn.setRequestProperty("Cookie", myCookie);
			httpConn.connect();
			String fname = httpConn.getHeaderField("Content-Disposition").split("\"",3)[1];
			if(httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				InputStream net_in = httpConn.getInputStream();
				FileOutputStream fid_out = new FileOutputStream("download/" + fname);
				byte buf[]=new byte[1024];
				int len;
				while((len=net_in.read(buf)) > 0)
					fid_out.write(buf,0,len);
				fid_out.close();
				System.out.printf("Info: File '%s' has been downloaded.\n",fname);
			}									
			else {
				System.err.printf("Error: File '%s' cannot be downloaded.\n",fname);
			}
			*/
		}
		/*
		catch (java.net.SocketTimeoutException e) {
			System.out.println("Cannot open download link.");
		}
		*/		
		catch(Exception e) {
			e.printStackTrace();
		} 
		finally {
			driver.close();
		}	
	}	
}
